package com.gpnu7_412.yanzhi.controller.management;

import com.github.pagehelper.PageInfo;
import com.gpnu7_412.yanzhi.dto.ConciseUserDto;
import com.gpnu7_412.yanzhi.dto.DataStatisticsDto;
import com.gpnu7_412.yanzhi.dto.DynamicDisplayDto;
import com.gpnu7_412.yanzhi.entity.Dynamic;
import com.gpnu7_412.yanzhi.entity.User;
import com.gpnu7_412.yanzhi.service.DynamicService;
import com.gpnu7_412.yanzhi.service.UserService;
import com.gpnu7_412.yanzhi.service.impl.DynamicServiceImpl;
import com.gpnu7_412.yanzhi.service.impl.UserServiceImpl;
import com.gpnu7_412.yanzhi.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZZH
 */
@CrossOrigin
@RestController
@RequestMapping("/management")
public class ManagementController {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserService userService;

    /**
     * 获取最近7天新增动态统计数据
     * @return 响应对象
     */
    @GetMapping("/statistics")
    public Result statistics() {
        LocalDateTime endDate = LocalDateTime.now().minusDays(1).withHour(23).withMinute(59).withSecond(59).withNano(999_999_999);
        LocalDateTime startDate = LocalDateTime.now().minusDays(7).withHour(0).withMinute(0).withSecond(0).withNano(0);
        List<DataStatisticsDto> dataStatisticsDtoList = dynamicService.getQuantityStatistics(startDate, endDate);
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", dataStatisticsDtoList.size());
        responseMap.put("list", dataStatisticsDtoList);
        return new Result(responseMap);
    }

    /**
     * 多条件查询用户
     * @return 响应对象
     */
    @PostMapping("/select/users")
    public Result selectUsersByCondition(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "3") Integer pageSize,
            @RequestBody Map<String, Object> requestMap) {
        System.out.println("pageNum:" + pageNum);
        System.out.println("pageSize:" + pageSize);
        User user = new User();
        String userId = (String) requestMap.get("userId");
        if (userId != null && !userId.isEmpty()) {
            user.setUserId(Integer.parseInt(userId));
        }
        user.setUserName((String) requestMap.get("userName"));
        user.setEmail((String) requestMap.get("email"));
        PageInfo<User> userPageInfo = userService.getUsersByCondition(pageNum, pageSize, user);
        return new Result(userPageInfo);
    }

    /**
     * 多条件查询动态
     * @return 响应对象
     */
    @PostMapping("/select/dynamics")
    public Result selectDynamicsByCondition(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
            @RequestBody Map<String, Object> requestMap) {
        Dynamic dynamic = new Dynamic();
        String dynamicId = (String) requestMap.get("dynamicId");
        if (dynamicId != null && !dynamicId.isEmpty()) {
            dynamic.setDynamicId(Integer.parseInt(dynamicId));
        }
        String userId = (String) requestMap.get("userId");
        if (userId != null && !userId.isEmpty()) {
            dynamic.setUserId(Integer.parseInt(userId));
        }
        dynamic.setTitle((String) requestMap.get("title"));
        PageInfo<DynamicDisplayDto> dynamicDisplayDtoPageInfo = dynamicService.getDynamicsByCondition(pageNum, pageSize, dynamic);
        return new Result(dynamicDisplayDtoPageInfo);
    }

    /**
     * 管理员删除用户
     * @param userId 用户ID
     * @return 响应对象
     */
    @DeleteMapping("/delete/user")
    public Result deleteUser(@RequestParam Integer userId) {
        userService.deleteUser(userId);
        return new Result();
    }
}
